<template>
    <el-form :title="title" :model="buyer" ref="buyerForm" :rules="rules">
        <el-form-item label="流水号" v-if="openState>0">
            <span>{{buyer.buyerCode}}</span>
        </el-form-item>

        <el-form-item label="昵称" prop="member.nickName" v-if="openState >=0">
            <el-input v-model="buyer.member.nickName"></el-input>
        </el-form-item>
        <el-form-item label="昵称" v-if="openState <0">
            <span>{{buyer.member.nickName}}</span>
        </el-form-item>

        <el-form-item label="余额" prop="member.balance" v-if="openState>=0">
            <el-input type="number" step="0.01"
                      v-model.number="buyer.member.balance"></el-input>
        </el-form-item>
        <el-form-item label="余额" v-if="openState <0">
            <span>{{buyer.member.balance}}</span>
        </el-form-item>

        <el-form-item label="注册时间" prop="createDate" v-if="openState===0">
            <el-date-picker value-format="yyyy-MM-dd" v-model="buyer.createDate"></el-date-picker>
        </el-form-item>
        <el-form-item label="注册时间" v-if="openState===1">
            <span>{{buyer.createDate}}</span>
        </el-form-item>
        <el-form-item>
            <el-button type="warning" @click="reset"
                       v-if="openState >=0">重 置</el-button>
            <el-button type="primary" @click="validateSubmit('put')"
                       v-if="openState ===0">保 存</el-button>
            <el-button type="primary" @click="validateSubmit('patch')"
                       v-if="openState>0">提 交</el-button>
            <el-button type="info" @click="silent"
                       v-if="openState>=0">取 消</el-button>
        </el-form-item>

    </el-form>

</template>

<script>
    export default {

        props: {
            title: {
                type: String,
                default: '一个实体 一个form标签来展示与操控 根据业务流程开放不同的显示模式'
            },
            openState: {
                type: Number,
                default: 0,
            },
            silent:{
                type: Function,
            },
            noise:{
                type: Function,
            }
        },
        data: function () {

            return {
                buyer: {
                    buyerCode: '000121',
                    createDate: new Date(),
                    id: 0,
                    member: {
                        balance: 1,
                        nickName: '张'
                    }
                },
                rules: {
                    member: {
                        balance: [
                            {required: true, message: '请输入剩余金额', trigger: 'blur'},
                            {type: 'number', message: '金额必须为数字值'}
                        ],
                        nickName: [
                            {required: true, message: '请输入昵称', trigger: 'blur'},
                            {min: 0, max: 10, message: '10个字符以内', trigger: 'blur'}
                        ],
                    },
                    createDate:[
                        {required:true,message:'请输入时间',trigger:'change'}
                    ]
                }
            }
        },
        computed: {},
        methods: {
            reset:function () {
                this.$refs['buyerForm'].resetFields();
            },
            validateSubmit:function (method) {
                var valid = this.$refs['buyerForm'].validate();
                if (!valid){
                    this.$message.warning("内容不正确"+JSON.stringify(this.buyer));
                    return 0;
                }
                if (!method) method='patch';
                method = method==='put'?'put':'patch';
                let url = 'http://localhost:8899/buyer';
                let content = method==='put'?this.buyer:this.buyer.member;
                url= method==='put'?url:url+'/'+this.buyer.buyerCode;
                this.$message.warning(method+'....  '+url);
                this.$http({
                    data:content,
                    method:method,
                    url:url,
                }).then(res=>{
                    this.buyer=res.data;
                    this.$emit('refresh');
                    this.silent();
                }).catch(this.$message.warning(JSON.stringify))
            },
            init:function (code) {
                let url = 'http://localhost:8899/buyer/'+code;
                this.$http({
                    method:'get',
                    url:url,
                }).then(res=>{
                    this.buyer=res.data;
                    this.$message.warning(JSON.stringify(this.buyer));
                    this.noise();
                }).catch(this.$message.warning(JSON.stringify));
            }
        },

        mounted: function () {
        },

        created: function () {

        },
        name: "BuyerDesc"
    }
</script>

<style scoped>

</style>
